package cn.backpackerxl.easyJDBC.main;

import cn.backpackerxl.easyJDBC.util.StringUtil;
import cn.backpackerxl.easyJDBC.util.MySQL;
import cn.backpackerxl.easyJDBC.util.SqlFactory;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @Author: backpackerxl
 * CreateTime: 2022/5/13
 * FileName: EditTableImpl
 **/
public class EditTableImpl {
    private EditTableImpl() {
    }

    /**
     * 根据字段修改表结构
     *
     * @param clazz
     * @param list
     */
    public static void doWithAlterTable(Class<?> clazz, List<String> list) {
        // alter table t_name drop column sex;
        String sql = String.format("%s %s %s %s %s ", MySQL.ALTER, MySQL.TABLE, SqlFactory.getTableName(clazz), MySQL.DROP, MySQL.COLUMN);
        for (String s : list) {
            Executor.doExecutorSQLUtil(sql + s);
        }
    }

    /**
     * 根据字段修改表结构
     *
     * @param clazz
     * @param key
     * @param mapFields
     */
    public static void doWithAlterTable(Class<?> clazz, List<String> key, Map<String, Field> mapFields) {
        // alter table t_name add column sex int(2);
        String sql = String.format("%s %s %s %s %s ", MySQL.ALTER, MySQL.TABLE, SqlFactory.getTableName(clazz), MySQL.ADD, MySQL.COLUMN);
        for (String s : key) {
            StringBuilder mid = new StringBuilder();
            Field field = mapFields.get(s);
            SqlFactory.getFieldSql(mid, "id", field);
            if (mid.length() == 0) {
                return;
            }
            Executor.doExecutorSQLUtil(sql + mid.toString().replace("NOT", "").replace(",", ""));
        }
    }

    public static void doWithAlterTable(Class<?> clazz, Map<String, String> noSome, Map<String, Field> mapFields) {
        // alter table t_name change username user varchar(20);
        String sql = String.format("%s %s %s %s", MySQL.ALTER, MySQL.TABLE, SqlFactory.getTableName(clazz), MySQL.CHANGE);
        // alter table t_name drop index IX_username;
        String sqlIX = String.format("%s %s %s %s %s ", MySQL.ALTER, MySQL.TABLE, SqlFactory.getTableName(clazz), MySQL.DROP, MySQL.INDEX);
        Map<String, String> indexMap = BasicQuery.indexByTable(clazz);
        noSome.forEach((key, value) -> {
            StringBuilder mid = new StringBuilder();
            Field field = mapFields.get(key);
            SqlFactory.getFieldSql(mid, "id", field);
            if (mid.length() == 0) {
                return;
            }
            if (indexMap.containsKey(value)) {
                Executor.doExecutorSQLUtil(sqlIX + indexMap.get(value));
            }
            String not = mid.toString().replace("NOT", "").replace(",", "");
            Executor.doExecutorSQLUtil(String.format("%s %s %s", sql, value, not));
        });
    }

    /**
     * 批量创建索引
     *
     * @param clazz
     * @param indexName
     */
    public static void doWithAlterTableIndex(Class<?> clazz, String... indexName) {
        Map<String, String> in = BasicQuery.indexByTable(clazz);
        for (String s : indexName) {
            if (!in.containsKey(StringUtil.removeHump(s))) {
                //  alter table t_admin add index IX_name(name)
                String sqlIX = String.format("%s %s %s %s %s IX_%s(%s)", MySQL.ALTER, MySQL.TABLE, SqlFactory.getTableName(clazz), MySQL.ADD, MySQL.INDEX, s.toLowerCase(Locale.ROOT), StringUtil.removeHump(s));
                Executor.doExecutorSQLUtil(sqlIX);
            }
        }
    }

    /**
     * 创建单个索引
     *
     * @param clazz
     * @param indexName
     */
    public static void doWithAlterTableIndex(Class<?> clazz, String indexName) {
        Map<String, String> in = BasicQuery.indexByTable(clazz);
        if (!in.containsKey(StringUtil.removeHump(indexName))) {
            //  alter table t_admin add index IX_name(name)
            String sqlIX = String.format("%s %s %s %s %s IX_%s(%s)", MySQL.ALTER, MySQL.TABLE, SqlFactory.getTableName(clazz), MySQL.ADD, MySQL.INDEX, indexName.toLowerCase(Locale.ROOT), StringUtil.removeHump(indexName));
            Executor.doExecutorSQLUtil(sqlIX);
        }
    }
}
